<?php
    class StoryController extends GTL_Action {
         public function init() {
            parent::init();
            /* Initialize action controller here */
            $this->list_sess_unset('controller_story');
            $this->session_search_key = 'controller_story';
            $this->session_search_name = 'search_label';
        }
        public function indexAction() {
            $this->_helper->redirector('list', 'story','default');
        }

        public function viewAction() {
            if ($this->_getParam('st_id') && $this->_getParam('st_id') != '') {
                $st_id = $this->_getParam('st_id');
            } else {
                $this->_helper->redirector('list', 'story', 'default');
            }
            $storyModel = new Model_Story();
            $storyalldata = $storyModel->fetchEntryById($st_id);
            $this->view->storyalldata = $storyalldata;
        }
        public function listAction() {
            $errorMessage= Array();
            $successmessage = '';
            $where = '';
            $searchtype = '';
            $orderField = 'st_id';
            $sort = 'asc';


            /* Pagination Login */
            $itemsPerPageReviews = $this->PAGING_PER_PAGE;
            $currentPageReviews = 1;
            //PAGING_PER_PAGE
            if ($this->_getParam('page') && $this->_getParam('page') != '') {
                $currentPageReviews = $this->_getParam('page');
            }

            if ($this->_getParam('field') && $this->_getParam('field') != '') {
                $orderField = $this->_getParam('field');
            }

            if ($this->_getParam('sort') && $this->_getParam('sort') != '') {
                $sort = $this->_getParam('sort');
            }
            $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

            if ($this->_request->isPost()) {
                $postedData = $this->_request->getPost();
                //Multiple Delete Logic 
                if (isset($postedData['multiaction']) && ($postedData['multiaction'] != '')) {
                    $action = trim($postedData ['multiaction']);
                    if (isset($postedData['multicheck']) && count($postedData['multicheck']) > 0) {
                        $ids = implode(',', $_POST['multicheck']);
                        $result = $this->deleteAction($_POST['multicheck']);
                    } else {
                        $errorMessage[] = ' Please select atleast one checkbox! ';
                    }
                }
                /*search code here*/
                  
                
            }
            $where = "st_lgn_id = ".$this->USER->lgn_id;
            $storyModel = new Model_Story();
            $storysData = $storyModel->fetchStory($where, $itemsPerPageReviews, $offset, $orderField, $sort);
            $paginator = new GTL_Paginator();
            $paginator->setItemsPerPage($itemsPerPageReviews);
            $paginator->setItemsTotal($storyModel->getStoryCount($where));
            $paginator->setCurrentPage($currentPageReviews);
            $link = $this->view->url(array('controller' => 'story', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
            $paginator->paginate($link);

            $this->view->paginator = $paginator;
            $this->view->storysData = $storysData;
            $this->view->errorMessage = $errorMessage;
            $this->view->successmessage = $successmessage;
            $this->view->statusArray = $storyModel->getStatusArray();
            $this->view->searchtype = $searchtype;
        }
        public function deleteAction($ids = Array()) {
            //Load story Model 
            $storyModel = new Model_Story();
            if ($this->_getParam('st_id') && $this->_getParam('st_id') != '') {
                $st_id = $this->_getParam('st_id');
                $storyModel->deleteStory($st_id);
                $this->addSuccessMessage('Record Successfully Deleted');
               $this->_helper->redirector('list', 'story', 'default');
            } else {
                $storyModel->deleteStory($ids);
               $this->addSuccessMessage('Record Successfully Deleted');
                $this->_helper->redirector('list', 'story', 'default');
            }
        }
        public function formAction() {
     //Initialize
            $successmessage = '';
            $errorMessageArray = Array();
            $data = array();
            $storyData = Array();
            $this->view->mode = 'Add';

            //Load story Model
            $storyModel = new Model_Story();

            //load project model
            $projectModel = new Model_Project();
            $lastproject = $projectModel ->fetchProject("", 1, 0, 'proj_id', 'desc');
            $lastprojectId =$lastproject[0]['proj_id'];
            
            //Load story Form
            $storyForm = new Form_StoryForm();

            //Load the status array for the select box in form 
            $statusArray = $storyModel->getStatusArray();
            $storyForm->st_status->addMultiOptions($statusArray);

            //Check for the story Parameter
            $storyData['st_id'] = $this->_request->getParam('st_id');
            $st_id = (!empty($storyData['st_id'])) ? $storyData['st_id'] : null;
            if (!empty($st_id)) {
            //Set the Mode to Edit
            $this->view->mode = 'Edit';
            //Fetch data to be loaded in form 
            $data = $storyModel->fetchEntryById($st_id);
            }
            //Populate form 
            $this->view->form = $storyForm->populate($data);
            if ($this->getRequest()->isPost()) {
                //Check for Errors
                $postedData = $this->_request->getPost();
                if (!$storyForm->isValid($postedData)) {
                    $errorMessage = $storyForm->getMessages();
                    foreach ($errorMessage as $_err) {
                        foreach ($_err as $_val) {
                            $errorMessageArray[] = $_val;
                        }
                    }
                } else {
                
                        
                    if (count($errorMessageArray) > 0) {

                    } else {
                    
                          
                        if ($this->view->mode == 'Add') {
                            $storyData['st_createddate'] = date('Y-m-d H:i:s');
                        }
                        //story save
                        $storyData['st_id'] = $st_id;
                        $storyData['st_lgn_id'] = $this->USER->lgn_id;
                        $storyData['st_proj_id'] = $lastprojectId;
                        $storyData['st_note'] = $postedData['st_note'];
                        $storyData['st_time_slot'] = $postedData['st_time_slot'];
                        
                        $storyData['st_updateddate'] = date('Y-m-d H:i:s');
                        $storyData['st_status'] = $postedData['st_status'];
						

                        $storyModel->save($storyData, 'st_id');
                        if ($this->view->mode == 'Add') {
                            $successmessage = 'Story add successfully';
                        } else {
                            $successmessage = 'Story updated successfully';
                        }
                        $this->addSuccessMessage($successmessage);
                        $this->_helper->redirector('list', 'story', 'default');
                    }
                }
            }
            $this->view->storyid = $st_id;
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
            $this->view->form = $storyForm;


    }
    }?>